package org.nf.ssm.service.impl;

import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.nf.ssm.common.exception.ErrorsEnum;
import org.nf.ssm.common.exception.LoginException;
import org.nf.ssm.dao.AdminDao;
import org.nf.ssm.domain.Admin;
import org.nf.ssm.service.AdminService;
import org.springframework.stereotype.Service;

/**
 * @author zhu
 * @date 2024-10-14
 */
@Service("adminService")
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {

    private final AdminDao adminDao;

    @Override
    public Admin auth(Admin admin, HttpSession session) {
        Admin loginAdmin = adminDao.login(admin);
        if (loginAdmin != null) {
            session.setAttribute("loginAdmin", loginAdmin);
            return loginAdmin;
        }

        throw new LoginException(ErrorsEnum.LOGIN_EXCEPTION);
    }

    @Override
    public void addAdmin(Admin admin) {
        adminDao.addAdmin(admin);
    }

    @Override
    public PageInfo<Admin> getAllAdmin(Integer pageNum, Integer pageSize) {
        return new PageInfo<>(adminDao.getAllAdmin(pageNum, pageSize));
    }

    @Override
    public void deleteAdmin(Integer adminId) {
        adminDao.deleteAdmin(adminId);
    }

    @Override
    public void updateAdmin(Admin admin) {
        adminDao.updateAdmin(admin);
    }
}
